import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import com.base.xzw_lib.R
import com.google.android.material.snackbar.Snackbar

object SnackToast {

    // 默认配置
    private var defaultDuration = Snackbar.LENGTH_SHORT
    private var defaultBackgroundColor = Color.parseColor("#323232") // Material Design 深灰色
    private var defaultTextColor = Color.WHITE
    private var defaultActionColor = Color.parseColor("#FF4081") // Material Design 粉色
    private var defaultGravity = Gravity.BOTTOM
    private var defaultMargin = 16 // dp
    private var defaultCornerRadius = 4 // dp
    private var defaultTypeface: Typeface? = null

    // 初始化配置
    fun init(
        duration: Int = Snackbar.LENGTH_SHORT,
        @ColorInt backgroundColor: Int = Color.parseColor("#323232"),
        @ColorInt textColor: Int = Color.WHITE,
        @ColorInt actionColor: Int = Color.parseColor("#FF4081"),
        gravity: Int = Gravity.BOTTOM,
        margin: Int = 16,
        cornerRadius: Int = 4,
        typeface: Typeface? = null
    ) {
        defaultDuration = duration
        defaultBackgroundColor = backgroundColor
        defaultTextColor = textColor
        defaultActionColor = actionColor
        defaultGravity = gravity
        defaultMargin = margin
        defaultCornerRadius = cornerRadius
        defaultTypeface = typeface
    }

    // 显示基本 Snackbar
    fun show(
        view: View,
        message: String,
        duration: Int = defaultDuration,
        @ColorInt backgroundColor: Int = defaultBackgroundColor,
        @ColorInt textColor: Int = defaultTextColor,
        gravity: Int = defaultGravity,
        margin: Int = defaultMargin,
        cornerRadius: Int = defaultCornerRadius
    ) {
        val snackbar = Snackbar.make(view, message, duration)
        customizeSnackbar(
            snackbar,
            backgroundColor,
            textColor,
            gravity,
            margin,
            cornerRadius
        )
        snackbar.show()
    }

    // 显示带动作的 Snackbar
    fun showWithAction(
        view: View,
        message: String,
        actionText: String,
        action: (View) -> Unit,
        duration: Int = defaultDuration,
        @ColorInt backgroundColor: Int = defaultBackgroundColor,
        @ColorInt textColor: Int = defaultTextColor,
        @ColorInt actionColor: Int = defaultActionColor,
        gravity: Int = defaultGravity,
        margin: Int = defaultMargin,
        cornerRadius: Int = defaultCornerRadius
    ) {
        val snackbar = Snackbar.make(view, message, duration)
            .setAction(actionText, action)
            .setActionTextColor(actionColor)

        customizeSnackbar(
            snackbar,
            backgroundColor,
            textColor,
            gravity,
            margin,
            cornerRadius
        )
        snackbar.show()
    }

    // 显示带图标的 Snackbar
    fun showWithIcon(
        view: View,
        message: String,
        @DrawableRes iconRes: Int,
        duration: Int = defaultDuration,
        @ColorInt backgroundColor: Int = defaultBackgroundColor,
        @ColorInt textColor: Int = defaultTextColor,
        gravity: Int = defaultGravity,
        margin: Int = defaultMargin,
        cornerRadius: Int = defaultCornerRadius
    ) {
        val snackbar = Snackbar.make(view, message, duration)
        customizeSnackbar(
            snackbar,
            backgroundColor,
            textColor,
            gravity,
            margin,
            cornerRadius
        )

        // 添加图标
        snackbar.view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text).apply {
            setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0)
            compoundDrawablePadding = dpToPx(view.context, 8)
        }

        snackbar.show()
    }

//    // 显示成功提示
//    fun showSuccess(
//        view: View,
//        message: String,
//        duration: Int = defaultDuration,
//        gravity: Int = defaultGravity
//    ) {
//        showWithIcon(
//            view = view,
//            message = message,
//            iconRes = R.drawable.ic_success,
//            backgroundColor = Color.parseColor("#4CAF50"), // 绿色
//            textColor = Color.WHITE,
//            duration = duration,
//            gravity = gravity
//        )
//    }
//
//    // 显示错误提示
//    fun showError(
//        view: View,
//        message: String,
//        duration: Int = Snackbar.LENGTH_LONG,
//        gravity: Int = defaultGravity
//    ) {
//        showWithIcon(
//            view = view,
//            message = message,
//            iconRes = R.drawable.ic_error,
//            backgroundColor = Color.parseColor("#F44336"), // 红色
//            textColor = Color.WHITE,
//            duration = duration,
//            gravity = gravity
//        )
//    }
//
//    // 显示警告提示
//    fun showWarning(
//        view: View,
//        message: String,
//        duration: Int = defaultDuration,
//        gravity: Int = defaultGravity
//    ) {
//        showWithIcon(
//            view = view,
//            message = message,
//            iconRes = R.drawable.ic_warning,
//            backgroundColor = Color.parseColor("#FF9800"), // 橙色
//            textColor = Color.WHITE,
//            duration = duration,
//            gravity = gravity
//        )
//    }
//
//    // 显示信息提示
//    fun showInfo(
//        view: View,
//        message: String,
//        duration: Int = defaultDuration,
//        gravity: Int = defaultGravity
//    ) {
//        showWithIcon(
//            view = view,
//            message = message,
//            iconRes = R.drawable.ic_info,
//            backgroundColor = Color.parseColor("#2196F3"), // 蓝色
//            textColor = Color.WHITE,
//            duration = duration,
//            gravity = gravity
//        )
//    }

    // 自定义 Snackbar 样式
    private fun customizeSnackbar(
        snackbar: Snackbar,
        @ColorInt backgroundColor: Int,
        @ColorInt textColor: Int,
        gravity: Int,
        margin: Int,
        cornerRadius: Int
    ) {
        val snackbarView = snackbar.view
        val textView = snackbarView.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)

        // 设置背景（使用 GradientDrawable 实现圆角）
        val background = GradientDrawable()
        background.setColor(backgroundColor)
        background.cornerRadius = dpToPx(snackbarView.context, cornerRadius).toFloat()
        snackbarView.background = background

        // 设置文字颜色
        textView.setTextColor(textColor)

        // 设置字体
        defaultTypeface?.let { textView.typeface = it }

        // 设置位置和边距
        val layoutParams = snackbarView.layoutParams as ViewGroup.MarginLayoutParams
        val marginPx = dpToPx(snackbarView.context, margin)
        layoutParams.setMargins(marginPx, marginPx, marginPx, marginPx)

        // 设置重力（位置）
        when (gravity) {
            Gravity.TOP -> {
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
                (snackbarView.parent as? ViewGroup)?.let { parent ->
                    parent.clipToPadding = false
                }
            }
            Gravity.CENTER -> {
                layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
            }
            else -> {
                // 底部默认处理
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
            }
        }

        snackbarView.layoutParams = layoutParams
    }


    // 创建圆角背景Drawable
    private fun createRoundedBackground(
        @ColorInt backgroundColor: Int,
        @ColorInt borderColor: Int = Color.TRANSPARENT,
        borderWidth: Int = 0,
        cornerRadius: Float
    ): Drawable {
        return GradientDrawable().apply {
            shape = GradientDrawable.RECTANGLE
            setColor(backgroundColor)
            setStroke(borderWidth, borderColor)
            this.cornerRadius = cornerRadius
        }
    }

//如果您想要更复杂的背景样式（如边框、渐变等），可以使用以下扩展方法
//    // 在customizeSnackbar方法中使用
//    val background = createRoundedBackground(
//        backgroundColor = backgroundColor,
//        borderColor = Color.WHITE, // 可选边框颜色
//        borderWidth = dpToPx(snackbarView.context, 1), // 可选边框宽度
//        cornerRadius = dpToPx(snackbarView.context, cornerRadius).toFloat()
//    )
//    snackbarView.background = background


    // DP 转 PX 工具方法
    private fun dpToPx(context: android.content.Context, dp: Int): Int {
        return (dp * context.resources.displayMetrics.density).toInt()
    }
}